package org.daidai.abstrusewriting.homepage.hot.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.hot.adapters.MyProgremAdapter;
import org.daidai.abstrusewriting.homepage.hot.client.ClientApi;
import org.daidai.abstrusewriting.homepage.hot.model.ProgremTracks;
import org.daidai.abstrusewriting.homepage.top.PlayActivity;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProgramFragment extends BaseFragment implements AdapterView.OnItemClickListener {


    private String mAlbumId;
    private List<ProgremTracks.TrackItem> mList;
    private MyProgremAdapter mAdapter;

    public ProgramFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getProgremTracks(ProgremTracks progremTracks){
        List<ProgremTracks.TrackItem> trackItemList = progremTracks.getTrackItemList();
        mList.clear();
        mList.addAll(trackItemList);
        mAdapter.notifyDataSetChanged();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = null;
        ret = inflater.inflate(R.layout.fragment_program, container, false);
        Bundle bundle = getArguments();
        mAlbumId = bundle.getString("mAlbumId");
        ListView listView = (ListView) ret.findViewById(R.id.fragment_program_list);
        mList = new ArrayList<>();
        mAdapter = new MyProgremAdapter(getContext(),mList);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(this);
        return ret;
    }

    @Override
    public void onResume() {
        super.onResume();
        ClientApi.getProgremDetail("http://mobile.ximalaya.com/mobile/v1/album?albumId="+mAlbumId+"&device=android&pageId=null&pageSize=20");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public String getTitle() {
        return "节目";
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ProgremTracks.TrackItem trackItem = mList.get(position);
        Intent intent = new Intent(getContext(),PlayActivity.class);
        intent.putExtra("trackId",mList.get(position).getTrackId());
        intent.putExtra("uid",mList.get(position).getUid());
        startActivity(intent);
    }
}
